package com.bier.fishproject.activity;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.json.JSONObject;

import com.bier.fishproject.R;
import com.bier.fishproject.utils.Base64Str;
import com.bier.fishproject.utils.Constant;
import com.bier.fishproject.utils.MyThreadPool;
import com.bier.fishproject.utils.YLWWater;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ScrollView;
import android.widget.TextView;
/**
 * 账单详情
 * @author Administrator
 *
 */
public class BillDetailsActivity extends BaseActivity{

	private TextView tvSaleno,tvMarket,tvName,tvPosid,tvCard,tvCardType,tvPhone,tvUserName,tvEndType,tvTones,tvStartTime,
	tvEndTones,tvEndTime,tvStones,tvStime,tvPrice,tvPType,tvQmoney,tvMoney,tvHmoney,tvUrl,tvHotline;
	private ScrollView sc;
	//打印次数
	private int index;
	//流水号
	private String result;
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_bill_details);  
		initView();
	}
	
	/**
	 * 初始化方法
	 */
	private void initView(){
		tvSaleno=(TextView)findViewById(R.id.tv_abd_saleno);
		tvMarket=(TextView)findViewById(R.id.tv_abd_market);
		tvName=(TextView)findViewById(R.id.tv_abd_name);
		tvPosid=(TextView)findViewById(R.id.tv_abd_posid);
		tvCard=(TextView)findViewById(R.id.tv_abd_card);
		tvCardType=(TextView)findViewById(R.id.tv_abd_cardType);
		tvPhone=(TextView)findViewById(R.id.tv_abd_phone);
		tvUserName=(TextView)findViewById(R.id.tv_abd_userName);
		tvEndType=(TextView)findViewById(R.id.tv_abd_endType);
		tvTones=(TextView)findViewById(R.id.tv_abd_tones);
		tvStartTime=(TextView)findViewById(R.id.tv_abd_startTime);
		tvEndTones=(TextView)findViewById(R.id.tv_abd_endtones);
		tvEndTime=(TextView)findViewById(R.id.tv_abd_endTime);
		tvStones=(TextView)findViewById(R.id.tv_abd_stones);
		tvStime=(TextView)findViewById(R.id.tv_abd_sTime);
		tvPrice=(TextView)findViewById(R.id.tv_abd_price);
		tvPType=(TextView)findViewById(R.id.tv_abd_ptype);
		tvQmoney=(TextView)findViewById(R.id.tv_abd_qmoney);
		tvMoney=(TextView)findViewById(R.id.tv_abd_money);
		tvHmoney=(TextView)findViewById(R.id.tv_abd_hmoney);
		tvUrl=(TextView)findViewById(R.id.tv_abd_url);
		tvHotline=(TextView)findViewById(R.id.tv_abd_hotline);
		sc=(ScrollView)findViewById(R.id.sc_abd);
		Bundle bundle=getIntent().getExtras();
		if(bundle!=null){
			result=bundle.getString("result");
			if(!TextUtils.isEmpty(result)){
				loadView("数据加载中...");
				MyThreadPool.getInstent().submit(new Runnable() {
					public void run() {
						try {
							TTransport transport = new TSocket(Constant.IP, Constant.PORT, 5000);
							transport.open();
							TProtocol protocol = new TBinaryProtocol(transport);
							TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "YLWWater");
							YLWWater.Client client = new YLWWater.Client(mp1);
							String msg=client.reqDealDetail(result);
							transport.close();
							if(!TextUtils.isEmpty(msg)){
								final Message message=new Message();
								message.what=Constant.ONE;
								message.obj=msg;
								handler.sendMessage(message);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				 });
			}
		}
		//打印
		findViewById(R.id.tv_abd_print).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(index>0){
					loadView("开始打印...");
					MyThreadPool.getInstent().submit(new Runnable() {
						public void run() {
							try {
								TTransport transport = new TSocket(Constant.IP, Constant.PORT, 5000);
								transport.open();
								TProtocol protocol = new TBinaryProtocol(transport);
								TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "YLWWater");
								YLWWater.Client client = new YLWWater.Client(mp1);
								String msg=client.dealPrint(result);
								transport.close();
								if(!TextUtils.isEmpty(msg)){
									final Message message=new Message();
									message.what=Constant.THREE;
									message.obj=msg;
									handler.sendMessage(message);
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					 });
				}else{
					showShortToast("对不起，打印次数已用完！");
				}
			}
		});
		//返回
		findViewById(R.id.img_ac_next).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				BillDetailsActivity.this.finish();
			}
		});
	}
	
	
	@SuppressLint("HandlerLeak") private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			endView();
			String result=msg.obj.toString();
			if(msg.what==Constant.ONE){
				try{
					JSONObject json=new JSONObject(result);
					if(json.getInt("status")==0){
						sc.setVisibility(View.VISIBLE);
						JSONObject j=new JSONObject(json.getString("infos"));
						//获取剩余打印次数
						index=Integer.parseInt(j.getString("p_num"));
						tvSaleno.setText("流水号："+j.getString("saleno"));
						tvMarket.setText("市场名称："+Base64Str.getFromBase64(j.getString("market_name")));
						tvName.setTag("水表名称："+Base64Str.getFromBase64(j.getString("d_name")));
						tvPosid.setText("编号："+j.getString("posid"));
						tvCard.setText("使用卡号："+j.getString("card"));
						if(j.getString("card_type").equals("1")){
							tvCardType.setText("卡类型：主卡");
						}else{
							tvCardType.setText("卡类型：副卡");
						}
						tvPhone.setText("手机号码："+j.getString("phone"));
						tvUserName.setText("姓名："+Base64Str.getFromBase64(j.getString("username")));
						if(j.getString("stop_way").equals("0")){
							tvEndType.setText("结束方式：刷卡停止");
						}else if(j.getString("stop_way").equals("1")){
							tvEndType.setText("结束方式：超级用户停止");
						}else if(j.getString("stop_way").equals("2")){
							tvEndType.setText("结束方式：定额");
						}else if(j.getString("stop_way").equals("3")){
							tvEndType.setText("结束方式：定时");
						}else if(j.getString("stop_way").equals("4")){
							tvEndType.setText("结束方式：定量");
						}else if(j.getString("stop_way").equals("5")){
							tvEndType.setText("结束方式：余额不足");
						}
						tvTones.setText("开始吨数："+j.getString("start_tones"));
						tvStartTime.setText("开始时间："+j.getString("start_time"));
						tvEndTones.setText("结束吨数："+j.getString("stop_tones"));
						tvEndTime.setText("结束时间："+j.getString("stop_time"));
						tvStones.setText("使用吨数："+j.getString("tones"));
						tvStime.setText("使用时间："+j.getString("seconds")+"秒");
						if(j.getString("pay_type").equals("1")){
							tvPrice.setText("单价："+j.getString("p_tones")+"(顿/元)");
							tvPType.setText("计价方式：计量");
						}else if(j.getString("pay_type").equals("0")){
							tvPrice.setText("单价："+j.getString("p_second")+"(秒/顿)");
							tvPType.setText("计价方式：计时");
						}
						tvQmoney.setText(j.getString("be_money"));
						tvMoney.setText(j.getString("money"));
						tvHmoney.setText(j.getString("af_money"));
						tvUrl.setText(j.getString("url"));
						tvHotline.setText(j.getString("hotline"));
					}else{
						showShortToast(Base64Str.getFromBase64(json.getString("msg")));
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}else {
				try{
					JSONObject json=new JSONObject(result);
					if(json.getInt("status")==0){
						showShortToast("正在打印中！");
					}else{
						showShortToast(Base64Str.getFromBase64(json.getString("msg")));
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		};
	};
}
